package com.gupao.vip.bio;

import cn.hutool.core.convert.Convert;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;

/**
 * 同步阻塞I/O Server
 */
public class TimeServer {
    public static void main(String[] args) {
        int port = Constants.DEFAULT_PORT;
        if(Objects.nonNull(args) && args.length > 0){
            port = Convert.toInt(args[0], port);
        }
        try(ServerSocket server = new ServerSocket(port)){
            System.out.println("The time server is start in port : " + port);
            Socket socket = null;

            //创建I/O任务线程池
            /**
             * 资源占用是可控的，无论多少个客户端并发访问，都不会导致资源的耗尽和宕机
             */
            TimeServerHandlerExecutePool singleExecutor = new TimeServerHandlerExecutePool(50, 10000);
            while(true){
                socket = server.accept();
                singleExecutor.execute(new TimeServerHandler(socket));
                //new Thread(new TimeServerHandler(socket)).start();
            }
        }catch(IOException e){
            System.err.println("启动ServerSocket对象失败!" + e.getMessage());
        }
    }
}
